home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / Search.frm < prev    next >
Text File  |  1997-06-14  |  14KB  |  473 lines

  1. VERSION 5.00
  2. Object = "*\ADropStack.vbp"
  3. Begin VB.Form FSearch 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    ClientHeight    =   2640
  6.    ClientLeft      =   2640
  7.    ClientTop       =   2610
  8.    ClientWidth     =   6585
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   8.25
  12.       Charset         =   0
  13.       Weight          =   700
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    Icon            =   "Search.frx":0000
  19.    LinkTopic       =   "Form1"
  20.    MaxButton       =   0   'False
  21.    MinButton       =   0   'False
  22.    ScaleHeight     =   2640
  23.    ScaleWidth      =   6585
  24.    Begin DropStack.XDropStack dropWith 
  25.       Height          =   315
  26.       Left            =   1680
  27.       TabIndex        =   14
  28.       Top             =   720
  29.       Width           =   3375
  30.       _ExtentX        =   5953
  31.       _ExtentY        =   556
  32.       BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  33.          Name            =   "MS Sans Serif"
  34.          Size            =   8.25
  35.          Charset         =   0
  36.          Weight          =   400
  37.          Underline       =   0   'False
  38.          Italic          =   0   'False
  39.          Strikethrough   =   0   'False
  40.       EndProperty
  41.    End
  42.    Begin DropStack.XDropStack dropWhat 
  43.       Height          =   315
  44.       Left            =   1680
  45.       TabIndex        =   13
  46.       Top             =   240
  47.       Width           =   3375
  48.       _ExtentX        =   5953
  49.       _ExtentY        =   556
  50.       BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  51.          Name            =   "MS Sans Serif"
  52.          Size            =   8.25
  53.          Charset         =   0
  54.          Weight          =   400
  55.          Underline       =   0   'False
  56.          Italic          =   0   'False
  57.          Strikethrough   =   0   'False
  58.       EndProperty
  59.    End
  60.    Begin VB.CommandButton cmdReplace 
  61.       Caption         =   "&Replace"
  62.       Height          =   375
  63.       Left            =   5295
  64.       TabIndex        =   12
  65.       Top             =   1215
  66.       Width           =   1215
  67.    End
  68.    Begin VB.Frame frmMessage 
  69.       Height          =   1200
  70.       Left            =   204
  71.       TabIndex        =   10
  72.       Top             =   1116
  73.       Width           =   1935
  74.       Begin VB.Label lblMessage 
  75.          Height          =   735
  76.          Left            =   105
  77.          TabIndex        =   11
  78.          Top             =   315
  79.          Width           =   1725
  80.       End
  81.    End
  82.    Begin VB.CheckBox chkCase 
  83.       Caption         =   "Match Ca&se"
  84.       Height          =   300
  85.       Left            =   2520
  86.       TabIndex        =   5
  87.       Top             =   1875
  88.       Width           =   2400
  89.    End
  90.    Begin VB.CheckBox chkWord 
  91.       Caption         =   "Find Whole Word &Only"
  92.       Height          =   300
  93.       Left            =   2508
  94.       TabIndex        =   4
  95.       Top             =   1545
  96.       Width           =   2364
  97.    End
  98.    Begin VB.ComboBox cboDirection 
  99.       Height          =   300
  100.       ItemData        =   "Search.frx":0442
  101.       Left            =   3645
  102.       List            =   "Search.frx":044F
  103.       Style           =   2  'Dropdown List
  104.       TabIndex        =   3
  105.       Top             =   1170
  106.       Width           =   1215
  107.    End
  108.    Begin VB.CommandButton cmdHelp 
  109.       Caption         =   "&Help"
  110.       Height          =   375
  111.       Left            =   5280
  112.       TabIndex        =   7
  113.       Top             =   2160
  114.       Width           =   1215
  115.    End
  116.    Begin VB.CommandButton cmdAll 
  117.       Caption         =   " Rep&lace All"
  118.       Height          =   375
  119.       Left            =   5280
  120.       TabIndex        =   6
  121.       Top             =   1680
  122.       Width           =   1215
  123.    End
  124.    Begin VB.CommandButton cmdCancel 
  125.       Cancel          =   -1  'True
  126.       Caption         =   "Cancel"
  127.       Height          =   375
  128.       Left            =   5280
  129.       TabIndex        =   2
  130.       Top             =   600
  131.       Width           =   1215
  132.    End
  133.    Begin VB.CommandButton cmdFindNext 
  134.       Caption         =   "Find &Next"
  135.       Default         =   -1  'True
  136.       Height          =   375
  137.       Left            =   5280
  138.       TabIndex        =   1
  139.       Top             =   120
  140.       Width           =   1215
  141.    End
  142.    Begin VB.Label lblDirection 
  143.       Caption         =   "&Direction:"
  144.       Height          =   300
  145.       Left            =   2505
  146.       TabIndex        =   9
  147.       Top             =   1170
  148.       Width           =   975
  149.    End
  150.    Begin VB.Label lblWith 
  151.       Caption         =   "Replace &With: "
  152.       Height          =   252
  153.       Left            =   120
  154.       TabIndex        =   0
  155.       Top             =   708
  156.       Width           =   1284
  157.    End
  158.    Begin VB.Label lblWhat 
  159.       Caption         =   "&Find What:"
  160.       Height          =   372
  161.       Left            =   120
  162.       TabIndex        =   8
  163.       Top             =   240
  164.       Width           =   1344
  165.    End
  166. End
  167. Attribute VB_Name = "FSearch"
  168. Attribute VB_GlobalNameSpace = False
  169. Attribute VB_Creatable = False
  170. Attribute VB_PredeclaredId = True
  171. Attribute VB_Exposed = False
  172. Attribute VB_Ext_KEY = "RepId" ,"4FB51841-CEAF-11CF-A15E-00AA00A74D48-0073"
  173.  
  174.  
  175. Option Explicit
  176.  
  177. Private fSetFocus As Integer
  178. Private iFindStart As Integer
  179. Private fBottomFile As Boolean, fNotFirst As Boolean
  180. Private yControl2 As Double, yControl3 As Double
  181. Private yControl4 As Double, yControl5 As Double, yButton5 As Double
  182. Private fReplaceMode As Boolean
  183. ' Pointer for XEditor reference
  184. Private pEditor As Long
  185. Private fInCompleted As Boolean
  186.  
  187. Private ordReplaceStatus As Integer
  188. Const ordFirst = 0
  189. Const ordLast = 1
  190. Const ordFail = 2
  191.  
  192. Private Sub cboDirection_Click()
  193.     Editor.SearchOptionDirection = cboDirection.ListIndex
  194. End Sub
  195.  
  196. Private Sub Form_Initialize()
  197.     BugLocalMessage "FSearch Initialize"
  198. End Sub
  199.  
  200. Private Sub Form_Load()
  201. With Editor
  202.     BugLocalMessage "FSearch Load"
  203.     ' Initialize all control values from editor
  204.     DrawForm
  205.     .SearchActive = True
  206.     chkWord.Value = -.SearchOptionWord
  207.     chkCase.Value = -.SearchOptionCase
  208.     cboDirection.ListIndex = .SearchOptionDirection
  209.     fInCompleted = True
  210.     Dim i As Long
  211.     dropWhat.MaxCount = .FindWhatMax
  212.     For i = .FindWhatCount To 1 Step -1
  213.         dropWhat.Text = .FindWhat(i)
  214.     Next
  215.     dropWith.MaxCount = .ReplaceWithMax
  216.     For i = .ReplaceWithCount To 1 Step -1
  217.         dropWith.Text = .ReplaceWith(i)
  218.     Next
  219.     fInCompleted = False
  220.     Show vbModeless
  221. End With
  222. End Sub
  223.  
  224. Private Sub Form_Unload(Cancel As Integer)
  225.     BugLocalMessage "FSearch Unload"
  226.     Editor.SearchActive = False
  227.     Editor.SetFocus
  228. End Sub
  229.  
  230. Private Sub Form_Terminate()
  231.     BugLocalMessage "FSearch Terminate"
  232. End Sub
  233.  
  234. Friend Property Get Editor() As XEditor
  235.     Dim editorI As XEditor
  236.     ' Turn editorI into an illegal, uncounted interface pointer
  237.     CopyMemory editorI, pEditor, 4
  238.     ' Do NOT hit the End button here! You will crash!
  239.     ' Assign to legal reference (VB AddRefs it)
  240.     Set Editor = editorI
  241.     ' Still do NOT hit the End button! You will still crash!
  242.     ' Destroy the illegal reference
  243.     CopyMemory editorI, 0&, 4
  244.     ' OK, hit the End button if you must
  245.     ' Internal XEditor reference goes out of scope (VB Releases it)
  246. End Property
  247.  
  248. Friend Property Set Editor(ByVal editorA As XEditor)
  249.     ' Store an Editor pointer rather than an Editor object
  250.     ' No AddRef for storing the pointer
  251.     pEditor = ObjPtr(editorA)
  252. End Property
  253.  
  254. Friend Property Get ReplaceMode() As Boolean
  255.     ReplaceMode = fReplaceMode
  256. End Property
  257.  
  258. Friend Property Let ReplaceMode(ByVal fReplaceModeA As Boolean)
  259.     If fReplaceMode <> fReplaceModeA Then
  260.         fReplaceMode = fReplaceModeA
  261.     End If
  262. End Property
  263.  
  264. Private Sub chkWord_Click()
  265.     Static fInClick As Boolean
  266.     If fInClick Then Exit Sub
  267.     fInClick = True
  268.     Editor.SearchOptionWord = Not Editor.SearchOptionWord
  269.     fInClick = False
  270. End Sub
  271.  
  272. Sub SearchChange(Kind As ESearchEvent)
  273. With Editor
  274.     Select Case Kind
  275.     Case eseFindWhat
  276.         If dropWhat.Text <> .FindWhat Then
  277.             dropWhat.Text = .FindWhat
  278.         End If
  279.     Case eseReplaceWith
  280.         If dropWith.Text <> .ReplaceWith Then
  281.             dropWith.Text = .ReplaceWith
  282.         End If
  283.     Case eseCase
  284.         chkCase.Value = -.SearchOptionCase
  285.     Case eseWholeWord
  286.         chkWord.Value = -.SearchOptionWord
  287.     Case eseDirection
  288.         cboDirection.ListIndex = .SearchOptionDirection
  289.     End Select
  290. End With
  291. End Sub
  292.  
  293. Private Sub chkCase_Click()
  294.     Static fInClick As Boolean
  295.     If fInClick Then Exit Sub
  296.     fInClick = True
  297.     Editor.SearchOptionCase = Not Editor.SearchOptionCase
  298.     fInClick = False
  299. End Sub
  300.  
  301. Private Sub cmdAll_Click()
  302.     With Editor
  303.  
  304.         ' Save position and start search from first
  305.         Dim iPos As Integer, s As String
  306.  
  307.         ' Find first match, but don't highlight
  308.         iPos = .FindNext(dropWhat)
  309.         Do While iPos
  310.             iPos = .ReplaceNext(dropWhat, dropWith)
  311.         Loop
  312.  
  313.     End With
  314. End Sub
  315.  
  316. Private Sub cmdCancel_Click()
  317.     Unload Me
  318. End Sub
  319.  
  320. Private Sub cmdFindNext_Click()
  321. With Editor
  322.     Dim i As Integer
  323.     ' Must be something to find
  324.     If dropWhat.Text = sEmpty Then
  325.         dropWhat.SetFocus
  326.         Exit Sub
  327.     End If
  328.     ' When Replace user selects Next once, make it default
  329.     cmdFindNext.Default = True
  330.     ' Find next item
  331.     i = .FindNext(dropWhat)
  332.     ' Deal with failed search
  333.     If i = 0 Then
  334.         lblMessage.Caption = "Text not found"
  335.         .SelLength = 0
  336.     Else
  337.         lblMessage.Caption = "Text found: " & .Line & "," & .Column
  338.     End If
  339.     dropWhat.SetFocus
  340. End With
  341. End Sub
  342.  
  343. Private Sub cmdReplace_Click()
  344.     With Editor
  345.  
  346.         ' Turn Find dialog into Replace dialog
  347.         If Not fReplaceMode Then
  348.             fReplaceMode = True
  349.             DrawForm
  350.             If dropWhat.Text = sEmpty Then
  351.                 dropWhat.SetFocus
  352.             Else
  353.                 dropWith.SetFocus
  354.             End If
  355.             Exit Sub
  356.         End If
  357.  
  358.         ' Must be something to replace
  359.         If dropWhat.Text = sEmpty Then
  360.             dropWhat.SetFocus
  361.             Exit Sub
  362.         End If
  363.         
  364.         ' If you want to ask user to confirm empty replace string, do it here
  365.  
  366.         ' When user selects Replace once, make it default
  367.         cmdReplace.Default = True
  368.  
  369.         If .ReplaceNext(dropWhat, dropWith) Then
  370.             Call .FindNext(dropWhat)
  371.         Else
  372.             lblMessage.Caption = "Text not found"
  373.             dropWhat.SetFocus
  374.             Exit Sub
  375.         End If
  376.  
  377.     End With
  378. End Sub
  379.  
  380. Private Sub ReplaceText(sSrc As String, ByVal sReplace As String, iStart As Integer, iLen As Integer)
  381.     sSrc = Left$(sSrc, iStart - 1) & sReplace & Mid$(sSrc, iStart + iLen)
  382. End Sub
  383.  
  384. Private Sub dropWhat_Completed(Text As String)
  385.     If fInCompleted Then Exit Sub
  386.     fInCompleted = True
  387.     lblMessage.Caption = sEmpty
  388.     Editor.FindWhat = Text
  389.     fInCompleted = False
  390. End Sub
  391.  
  392. Private Sub dropWhat_Change()
  393.     lblMessage.Caption = sEmpty
  394. End Sub
  395.  
  396. Private Sub dropWith_Completed(Text As String)
  397.     If fInCompleted Then Exit Sub
  398.     fInCompleted = True
  399.     lblMessage.Caption = sEmpty
  400.     Editor.ReplaceWith = Text
  401.     fInCompleted = False
  402. End Sub
  403.  
  404. Private Sub dropWith_Change()
  405.     lblMessage.Caption = Empty
  406. End Sub
  407.  
  408. Private Sub DrawForm()
  409.  
  410.     ' Get initial button and control positions for later placement
  411.     If fNotFirst = False Then
  412.         fNotFirst = True
  413.         yControl2 = dropWith.Top
  414.         yControl3 = cboDirection.Top
  415.         yControl4 = chkWord.Top
  416.         yControl5 = chkCase.Top
  417.         yButton5 = cmdHelp.Top
  418.     End If
  419.  
  420.     ' Modify buttons and controls for current mode
  421.     If fReplaceMode = False Then
  422.         cmdFindNext.Caption = "&Next"
  423.         cmdFindNext.Default = True
  424.         cmdCancel.Caption = "&Close"
  425.         cmdReplace.Caption = "&Replace..."
  426.         Caption = "Find"
  427.         cmdAll.Visible = False
  428.         lblWith.Visible = False
  429.         dropWith.Visible = False
  430.         cmdHelp.Top = cmdAll.Top
  431.         frmMessage.Top = yControl2
  432.         cboDirection.Top = yControl2
  433.         lblDirection.Top = yControl2
  434.         chkWord.Top = yControl3
  435.         chkCase.Top = yControl4
  436.     Else ' Replace
  437.         cmdCancel.Caption = "&Close"
  438.         cmdReplace.Default = True
  439.         cmdFindNext.Caption = "Find &Next"
  440.         cmdReplace.Caption = "&Replace"
  441.         Caption = "Replace"
  442.         cmdAll.Visible = True
  443.         lblWith.Visible = True
  444.         dropWith.Visible = True
  445.         cmdHelp.Top = yButton5
  446.         frmMessage.Top = yControl3
  447.         cboDirection.Top = yControl3
  448.         lblDirection.Top = yControl3
  449.         chkWord.Top = yControl4
  450.         chkCase.Top = yControl5
  451.     End If
  452.  
  453.     Dim dyTitleBar As Double, dyBtnLow As Double, dyBorder As Double
  454.     ' Calculate height of title bar
  455.     dyTitleBar = Height - ScaleHeight
  456.     ' Add height of lowest element (help button moves up and down)
  457.     dyBtnLow = cmdHelp.Top + cmdHelp.Height
  458.     ' Add border around closest element (top button)
  459.     dyBorder = cmdFindNext.Top
  460.     ' Set height
  461.     Height = dyTitleBar + dyBtnLow + dyBorder
  462.  
  463. End Sub
  464.  
  465. Public Property Get Foo() As Long
  466.     Foo = 10
  467. End Property
  468.  
  469. Friend Property Get Bar() As Long
  470.     Bar = 10
  471. End Property
  472.  
  473.